package org.bigmarketapi.projectreview.DesingMode.DecoratorPattern.Update;

import org.bigmarketapi.projectreview.DesingMode.DecoratorPattern.Basic.RobotDecorator;
import org.bigmarketapi.projectreview.DesingMode.DecoratorPattern.Robot;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: ProjectReview
 * @BelongsPackage: org.bigmarketapi.projectreview.DesingMode.DecoratorPattern.Update
 * @Author: dujiaqi
 * @CreateTime: 2024-11-16  17:04
 * @Description: TODO
 * @Version: 1.0
 */
public class DynamicRobotDecorator implements Robot {
    private Robot robot;
    private List<abstractRobotDecorator> decorators = new ArrayList<>();

    public DynamicRobotDecorator(Robot robot) {
        this.robot = robot;
    }

    public void addDecorator(abstractRobotDecorator decorator) {
        decorators.add(decorator);
    }

    public void removeDecorator(abstractRobotDecorator decorator) {
        decorators.remove(decorator);
    }

    @Override
    public void doSomeThing() {
        Robot current = this.robot;
        for (abstractRobotDecorator decorator : decorators) {
            current = decorator.decorator(current);
        }
        current.doSomeThing();
    }

    public static Robot decorator(Robot robot, abstractRobotDecorator... abstractRobotDecorator) {
        final DynamicRobotDecorator dynamicRobotDecorator = new DynamicRobotDecorator(robot);
        for (abstractRobotDecorator decorator : abstractRobotDecorator) {
            {
                dynamicRobotDecorator.addDecorator(decorator);
            }
        }
        return dynamicRobotDecorator;
    }
}
